package com.mi.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mi.common.core.util.Result;
import com.mi.common.log.annotation.Log;
import com.mi.common.log.constant.enums.ServiceIdEnum;
import com.mi.system.dto.query.LogQueryDTO;
import com.mi.system.service.LogService;
import com.mi.system.vo.LogServiceVO;
import com.mi.system.vo.LogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.mi.common.core.util.Result.ok;

/**
 * Log controller
 *
 * @author jin_huaquan
 * @version 1.0
 * @date 2021/11/1 15:06
 */
@Api(value = "操作日志API", tags = "Log")
@ApiSort(900)
@RestController
@RequestMapping("/api/v1/logs")
@RequiredArgsConstructor
@Slf4j
public class LogController {

    private final LogService logService;

    @ApiOperation(value = "查询操作日志类别列表", notes = "查询操作日志类别列表")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 1)
    @Log(title = "查询操作日志类别列表")
    @GetMapping("/services/actions/list")
    public Result<List<LogServiceVO>> listService() {
        log.info("API CALL : list log service.");

        List<LogServiceVO> voList = Arrays.stream(ServiceIdEnum.values())
                .map(this::toVO)
                .collect(Collectors.toList());

        return ok(voList);
    }


    @ApiOperation(value = "分页查询操作日志列表", notes = "分页查询操作日志列表")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 2)
    @Log(title = "查询操作日志列表")
    @GetMapping("/actions/list")
    public Result<IPage<LogVO>> listDept(Page page, LogQueryDTO query) {
        log.info("API CALL : list log.");

        return ok(logService.listLog(page, query));
    }

    /**
     * Service id enum map to vo
     *
     * @param service service
     * @return vo
     */
    private LogServiceVO toVO(ServiceIdEnum service) {
        LogServiceVO vo = new LogServiceVO();

        vo.setServiceId(service.getServiceId());
        vo.setDesc(service.getDesc());

        return vo;
    }
}
